package com.koko.service.impl;

import com.koko.constant.FileNameConstant;
import com.koko.game.entity.GameDetail;
import com.koko.mapper.GameDetailMapper;
import com.koko.properties.HttpParamsProperties;
import com.koko.properties.HttpPathProperties;
import com.koko.service.GameDetailService;
import com.koko.utils.HttpUrlUtil;
import com.koko.utils.IOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class GameDetailServiceImpl implements GameDetailService {
    @Autowired
    private GameDetailMapper gameDetailMapper;

    @Value("${koko.urlPrefix}")
    private String baseUrl;

    @Value("${koko.path.game}")
    private String basePath;

    @Override
    public String getSimplePictureUrl(Long gameId) {
        GameDetail gameDetail = gameDetailMapper.getByGameId(gameId);
        return HttpUrlUtil.imageRequestUrlEncapsulate(baseUrl + HttpPathProperties.GAME_PICTURE_DOWNLOAD,
                HttpParamsProperties.GAME_PICTURE_ID + gameId,
                HttpParamsProperties.GAME_PICTURE_NAME + gameDetail.getSimplePicture());
    }

    @Override
    @Transactional
    public void save(GameDetail gameDetail,
                     MultipartFile widerPicture,
                     MultipartFile higherPicture,
                     MultipartFile simplePicture,
                     MultipartFile post) {
        // 1. 存储图片
        String widerPictureFileName = IOUtil.saveFile(widerPicture,
                basePath + gameDetail.getId() + "/",
                FileNameConstant.GAME_WIDER_PICTURE);
        String higherPictureFileName = IOUtil.saveFile(higherPicture,
                basePath + gameDetail.getGameId() + "/",
                FileNameConstant.GAME_HIGHER_PICTURE);
        String simplePictureFileName = IOUtil.saveFile(simplePicture,
                basePath + gameDetail.getGameId() + "/",
                FileNameConstant.GAME_SIMPLE_PICTURE);
        String postFileName = IOUtil.saveFile(post,
                basePath + gameDetail.getGameId() + "/",
                FileNameConstant.GAME_POST);
        // 2. 存储文本
        String IntroductionFileName = IOUtil.saveStringToTxt(gameDetail.getIntroduction(),
                basePath + gameDetail.getGameId() + "/",
                FileNameConstant.GAME_INTRODUCTION);
        String description = IOUtil.saveStringToTxt(gameDetail.getIntroduction(),
                basePath + gameDetail.getGameId() + "/",
                FileNameConstant.GAME_DESCRIPTION);
        // 3. 将文件名写入对象
        gameDetail.setWiderPicture(widerPictureFileName);
        gameDetail.setHigherPicture(higherPictureFileName);
        gameDetail.setSimplePicture(simplePictureFileName);
        gameDetail.setPost(postFileName);
        gameDetail.setIntroduction(IntroductionFileName);
        gameDetail.setIntroduction(description);
        // 4. 操作数据库
        gameDetailMapper.insert(gameDetail);
    }

    @Override
    public void deleteByGameId(Long gameId) {
        gameDetailMapper.deleteByGameId(gameId);
    }

    @Override
    @Transactional
    public void update(GameDetail gameDetail,
                       MultipartFile widerPicture,
                       MultipartFile higherPicture,
                       MultipartFile simplePicture,
                       MultipartFile post) {
        GameDetail gameDetailInDB = gameDetailMapper.getByGameId(gameDetail.getGameId());
        // 1. 修改图片
        if (widerPicture != null) {
            // 删除原图片
            IOUtil.deleteFileOrDirectory(basePath + gameDetailInDB.getGameId() + "/" + gameDetailInDB.getWiderPicture());
            String widerPictureFileName = handlePictureUpdate(widerPicture, gameDetailInDB);
            gameDetailInDB.setWiderPicture(widerPictureFileName);
        }
        if (higherPicture != null) {
            // 删除原图片
            IOUtil.deleteFileOrDirectory(basePath + gameDetailInDB.getGameId() + "/" + gameDetailInDB.getHigherPicture());
            String higherPictureFileName = handlePictureUpdate(higherPicture, gameDetailInDB);
            gameDetailInDB.setHigherPicture(higherPictureFileName);
        }
        if (simplePicture != null) {
            // 删除原图片
            IOUtil.deleteFileOrDirectory(basePath + gameDetailInDB.getGameId() + "/" + gameDetailInDB.getSimplePicture());
            String simplePictureFileName = handlePictureUpdate(simplePicture, gameDetailInDB);
            gameDetailInDB.setSimplePicture(simplePictureFileName);
        }
        if (post != null) {
            // 删除原图片
            IOUtil.deleteFileOrDirectory(basePath + gameDetailInDB.getGameId() + "/" + gameDetailInDB.getPost());
            String postFileName = handlePictureUpdate(post, gameDetailInDB);
            gameDetailInDB.setPost(postFileName);
        }
        // 2. 修改文本
        if (gameDetail.getIntroduction() != null && !gameDetail.getIntroduction().isEmpty()) {
            String fileName = IOUtil.saveStringToTxt(gameDetail.getIntroduction(),
                    basePath + gameDetailInDB.getGameId() + "/",
                    gameDetailInDB.getIntroduction());
            gameDetailInDB.setIntroduction(fileName);
        }
        if (gameDetail.getIntroduction() != null && !gameDetail.getIntroduction().isEmpty()) {
            String fileName = IOUtil.saveStringToTxt(gameDetail.getDescription(),
                    basePath + gameDetailInDB.getGameId() + "/",
                    gameDetailInDB.getDescription());
            gameDetailInDB.setDescription(fileName);
        }
        // 3. 操作数据库
        gameDetailMapper.updateByGameId(gameDetailInDB);
    }

    @Override
    public GameDetail getByGameId(Long gameId) {
        GameDetail gameDetail = gameDetailMapper.getByGameId(gameId);
        // 封装图片url
        gameDetail.setSimplePicture(encapsulateImageUrl(gameDetail, gameDetail.getSimplePicture()));
        gameDetail.setWiderPicture(encapsulateImageUrl(gameDetail, gameDetail.getWiderPicture()));
        gameDetail.setHigherPicture(encapsulateImageUrl(gameDetail, gameDetail.getHigherPicture()));
        gameDetail.setPost(encapsulateImageUrl(gameDetail, gameDetail.getPost()));
        // 从问价中读取文本
        try {
            String intro = IOUtil.readFileToString(basePath + gameId + "/" + gameDetail.getIntroduction());
            String desc = IOUtil.readHtmlContentFromTxt(basePath + gameId + "/" + gameDetail.getDescription());
            gameDetail.setIntroduction(intro);
            gameDetail.setDescription(desc);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return gameDetail;
    }

    @Override
    public void add(GameDetail gameDetail,
                    MultipartFile widerPicture,
                    MultipartFile higherPicture,
                    MultipartFile simplePicture,
                    MultipartFile post) {
        // 1. 添加图片
        String widerPictureFileName = handlePictureUpdate(widerPicture, gameDetail);
        gameDetail.setWiderPicture(widerPictureFileName);
        String higherPictureFileName = handlePictureUpdate(higherPicture, gameDetail);
        gameDetail.setHigherPicture(higherPictureFileName);
        String simplePictureFileName = handlePictureUpdate(simplePicture, gameDetail);
        gameDetail.setSimplePicture(simplePictureFileName);
        String postFileName = handlePictureUpdate(post, gameDetail);
        gameDetail.setPost(postFileName);
        // 2. 添加介绍和描述
        String introductionFileName = IOUtil.saveStringToTxt(gameDetail.getIntroduction(),
                basePath + gameDetail.getGameId() + "/",
                FileNameConstant.GAME_INTRODUCTION);
        gameDetail.setIntroduction(introductionFileName);
        String descriptionFileName = IOUtil.saveStringToTxt(gameDetail.getDescription(),
                basePath + gameDetail.getGameId() + "/",
                FileNameConstant.GAME_DESCRIPTION);
        gameDetail.setDescription(descriptionFileName);
        // 3. 操作数据库
        gameDetailMapper.insert(gameDetail);
    }

    private String handlePictureUpdate(MultipartFile newPicture, GameDetail gameDetail) {
        // 将新图片写入磁盘中
        String pictureFileName = IOUtil.saveFile(newPicture,
                basePath + gameDetail.getGameId() + "/");
        return pictureFileName;
    }

    private String encapsulateImageUrl(GameDetail gameDetail, String imageFileName) {
        return HttpUrlUtil.imageRequestUrlEncapsulate(baseUrl + HttpPathProperties.GAME_PICTURE_DOWNLOAD,
                HttpParamsProperties.GAME_PICTURE_ID + gameDetail.getGameId(),
                HttpParamsProperties.GAME_PICTURE_NAME + imageFileName);
    }
}
